package com.fs.cv.controller;

import com.fs.cv.pojo.pojodao.Absence;
import com.fs.cv.pojo.pojoview.AbsenceCourse;
import com.fs.cv.service.AbsenceService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * @description:
 * @author: hj
 * @createDate: 2020/9/28
 * @version: 1.0
 */
@Controller
@RequestMapping("/absenceController")
public class AbsenceController {
    @Autowired
    private AbsenceService absenceService;

    /**
     * 方法描述
     *
     * @param
     * @return 返回到考勤页面
     * @author 侯俊
     * @date 2020/9/29
     */
    @RequestMapping("/toabsence")
    public String getListView() {
        //直接返回jsp页面
        return "absence";
    }

    //分页显示
    @RequestMapping("/absenceByPage")
    @ResponseBody
    public Map<String, Object> getlistByPage(Integer currentPage, Integer pageSize) {
        //获取分页后的查询结果
        PageInfo<AbsenceCourse> pageInfo = absenceService.getAbsenceByPage(currentPage, pageSize);
        Map<String, Object> result = new HashMap<>();
        result.put("code", 0);
        result.put("absencelist", pageInfo.getList()); // 返回查询出来的分页数据
        result.put("totalPage", pageInfo.getPages()); //返回当前总共有多少页
        System.out.println(pageInfo.getPages() + "*******");
        return result;
    }

    //增加缺勤学生的信息
    @RequestMapping("/addOne")
    @ResponseBody
    public Map<String, Object> addOne(String studentName, String courseName, String absenceTime) {
        Map<String, Object> result = new HashMap<>();
        //根据学生姓名查询学生ID
        Integer studentId = absenceService.getBySname(studentName);
        System.out.println(studentId+"--------------------------");
        //根据课程名称查询课程ID
        Integer courseId = absenceService.getByCname(courseName);
        //创建缺课学生信息
        Absence absence = new Absence(0, studentId, studentName, courseId, absenceTime);
        int i = absenceService.addOne(absence);
        if (i > 0) {
            result.put("code", 0);
            result.put("message","登记成功");
        }else {
            result.put("code",-1);
            result.put("message","登记失败");
        }
        return result;
    }
        //根据学号来删除缺勤学生
        @RequestMapping("/deleted")
        @ResponseBody
        public Map<String,Object> deleted(int absenceId){
            Map<String, Object> result = new HashMap<>();
            int i = absenceService.deletedOne(absenceId);
            if (i > 0) {
                result.put("code", 0);
                result.put("message", "删除成功");
            } else {
                result.put("code", -1);
                result.put("message", "删除失败");
            }
            return result;
        }
        //模糊查询
    @RequestMapping("/seletedByName")
    @ResponseBody
    public Map<String,Object> seletedByName(String studentName){
        Map<String, Object> result = new HashMap<>();
        Set<AbsenceCourse> absence = absenceService.getByName(studentName);
        System.out.println(absence+"-------------------------------");
        if (absence!=null && !absence.isEmpty()){
            result.put("code",0);
            result.put("absence",absence);
            result.put("message","搜索成功");
        }else {
            result.put("code",-1);
            result.put("message","本班无此人");
        }
        return result;

    }
}


